package top.superxuqc.loginbili.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import top.superxuqc.loginbili.service.DownVideoService;
import top.superxuqc.loginbili.util.MyHttpClient;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@Controller
public class PlayVideo {
    @Autowired
    private DownVideoService ds;

    /**
     * 无法获取 弃用
     * @param av
     * @param request
     * @param response
     */
    @Deprecated
    public void flv(String av, HttpServletRequest request, HttpServletResponse response){
        JSONArray video = ds.getFlvVideo(av, request);
        String url = "https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/08/39/334883908/334883908_nb2-1-30116.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1621413420&gen=playurlv2&os=coso1bv&oi=2102830153&trid=2258313bf3b0435ca162480a148e8d09u&platform=pc&upsig=615ff7aa224169ccef2f6b1b5a12b040&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=229162729&orderid=0,3&agrr=1&logo=80000000";
        url = video.getJSONArray(0).getJSONObject(0).getString("url");
        System.out.println(url);
        ds.getFlvStream(url,request,response);
//        OutputStream outputStream = null;
//        response.addHeader("Content-Type","video/mp4;charset=UTF-8");
//
//        try {
//            outputStream = response.getOutputStream();
//            IOUtils.copy(stream,outputStream);
//            response.flushBuffer();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            outputStream = response.getOutputStream();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//
//            int len = 0;
//            byte[] buf = new byte[1024];
//            while ((len = stream.read(buf, 0, 1024)) != -1) {
//                outputStream.write(buf, 0, len);
//            }
//            outputStream.flush();
//            stream.close();
//            outputStream.close();
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            System.out.println("出错" + e.getMessage());
//            e.printStackTrace();
//        }



    }

    /**
     * 无法获取 弃用
     * @param av
     * @param request
     * @param response
     */
    @Deprecated
    @RequestMapping(value = "/x/v/video")
    public void video(String av, HttpServletRequest request, HttpServletResponse response){
        JSONArray video = ds.getVideo(av, request);
        String url = "https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/08/39/334883908/334883908_nb2-1-30116.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1621413420&gen=playurlv2&os=coso1bv&oi=2102830153&trid=2258313bf3b0435ca162480a148e8d09u&platform=pc&upsig=615ff7aa224169ccef2f6b1b5a12b040&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=229162729&orderid=0,3&agrr=1&logo=80000000";
        InputStream stream = ds.getStream(url,request);

        OutputStream outputStream = null;

        try {
            outputStream = response.getOutputStream();
            IOUtils.copy(stream,outputStream);
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }




    }





    /**
     *
     * @param quality 0:192k 1:132k 2:64k
     * @param av 视频av或者bv号
     * @param request
     * @param response
     */
    @RequestMapping(value = "/x/audio")
    public void audio(@RequestParam(defaultValue="0",required=false) int quality, String av, HttpServletRequest request, HttpServletResponse response){
        //String url ="https://xy14x104x230x146xy.mcdn.bilivideo.cn:4483/upgcxcode/08/39/334883908/334883908_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1621410514&gen=playurlv2&os=mcdn&oi=2102830156&trid=0001f9096b402e774dc7868216b4c01fa236u&platform=pc&upsig=4f68bc72eb343f45143d8e28f14acd6b&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mcdnid=2000740&mid=229162729&orderid=0,3&agrr=1&logo=A0000002";
        System.out.println("访问音频");
        JSONArray audio = ds.getVideo(av, request);
        String url30280 = "",url30216 = "",url30232 = "";
        if(audio.size() > 1){
            //TODO 多p音频
        }else {

            JSONObject audio1 = audio.getJSONObject(0).getJSONArray("audio").getJSONObject(0);
            JSONObject audio2 = audio.getJSONObject(0).getJSONArray("audio").getJSONObject(1);
            JSONObject audio3 = audio.getJSONObject(0).getJSONArray("audio").getJSONObject(2);
            switch (audio1.getInteger("id")){
                case 30280:
                    url30280 = audio1.getString("baseUrl");
                    break;
                case 30216:
                    url30216 = audio1.getString("baseUrl");
                    break;
                case 30232:
                    url30232 = audio1.getString("baseUrl");
                    break;
                default:
                    throw new NullPointerException("此音频质量无配置");
            }
            switch (audio2.getInteger("id")){
                case 30280:
                    url30280 = audio2.getString("baseUrl");
                    break;
                case 30216:
                    url30216 = audio2.getString("baseUrl");
                    break;
                case 30232:
                    url30232 = audio2.getString("baseUrl");
                    break;
                default:
                    throw new NullPointerException("此音频质量无配置");
            }
            switch (audio3.getInteger("id")){
                case 30280:
                    url30280 = audio3.getString("baseUrl");
                    break;
                case 30216:
                    url30216 = audio3.getString("baseUrl");
                    break;
                case 30232:
                    url30232 = audio3.getString("baseUrl");
                    break;
                default:
                    throw new NullPointerException("此音频质量无配置");
            }


        }
        String url = "";
        switch (quality){
            case 0:
                url = url30280;
                break;
            case 1:
                url = url30232;
                break;
            case 2:
                url = url30216;
                break;
        }
        ds.getAudio(url,request,response);



    }


    @RequestMapping(value = "/x/video")
    public void video(@RequestParam(defaultValue="0",required=false) int quality, String av, HttpServletRequest request, HttpServletResponse response){
        //String url ="https://xy14x104x230x146xy.mcdn.bilivideo.cn:4483/upgcxcode/08/39/334883908/334883908_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1621410514&gen=playurlv2&os=mcdn&oi=2102830156&trid=0001f9096b402e774dc7868216b4c01fa236u&platform=pc&upsig=4f68bc72eb343f45143d8e28f14acd6b&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mcdnid=2000740&mid=229162729&orderid=0,3&agrr=1&logo=A0000002";
        System.out.println("视频");
        JSONArray video = ds.getVideo(av, request);
        String url = "";
        if(video.size() > 1){
            //TODO 多p频
        }else {

            JSONObject video1 = video.getJSONObject(0).getJSONArray("video").getJSONObject(0);
            JSONObject video2 = video.getJSONObject(0).getJSONArray("video").getJSONObject(2);
            JSONObject video3 = video.getJSONObject(0).getJSONArray("video").getJSONObject(4);
            switch (quality){
                case 0:
                    url = video1.getString("baseUrl");
                    break;
                case 1:
                    url = video2.getString("baseUrl");
                    break;
                case 2:
                    url = video3.getString("baseUrl");
                    break;
            }


        }


        ds.getVideo(url,request,response);




    }

    @RequestMapping(value = "/x/down")
    public void downVideo(@RequestParam(defaultValue="0",required=false) int quality, String av, HttpServletRequest request, HttpServletResponse response){
        //String url ="https://xy14x104x230x146xy.mcdn.bilivideo.cn:4483/upgcxcode/08/39/334883908/334883908_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1621410514&gen=playurlv2&os=mcdn&oi=2102830156&trid=0001f9096b402e774dc7868216b4c01fa236u&platform=pc&upsig=4f68bc72eb343f45143d8e28f14acd6b&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mcdnid=2000740&mid=229162729&orderid=0,3&agrr=1&logo=A0000002";
        System.out.println("音频down");

        JSONArray audio = ds.getVideo(av, request);
        String url30280 = "",url30216 = "",url30232 = "";
        if(audio.size() > 1){
            //TODO 多p音频
        }else {

            JSONObject audio1 = audio.getJSONObject(0).getJSONArray("audio").getJSONObject(0);
            JSONObject audio2 = audio.getJSONObject(0).getJSONArray("audio").getJSONObject(1);
            JSONObject audio3 = audio.getJSONObject(0).getJSONArray("audio").getJSONObject(2);
            switch (audio1.getInteger("id")){
                case 30280:
                    url30280 = audio1.getString("baseUrl");
                    break;
                case 30216:
                    url30216 = audio1.getString("baseUrl");
                    break;
                case 30232:
                    url30232 = audio1.getString("baseUrl");
                    break;
                default:
                    throw new NullPointerException("此音频质量无配置");
            }
            switch (audio2.getInteger("id")){
                case 30280:
                    url30280 = audio2.getString("baseUrl");
                    break;
                case 30216:
                    url30216 = audio2.getString("baseUrl");
                    break;
                case 30232:
                    url30232 = audio2.getString("baseUrl");
                    break;
                default:
                    throw new NullPointerException("此音频质量无配置");
            }
            switch (audio3.getInteger("id")){
                case 30280:
                    url30280 = audio3.getString("baseUrl");
                    break;
                case 30216:
                    url30216 = audio3.getString("baseUrl");
                    break;
                case 30232:
                    url30232 = audio3.getString("baseUrl");
                    break;
                default:
                    throw new NullPointerException("此音频质量无配置");
            }


        }
        String url = "";
        switch (quality){
            case 0:
                url = url30280;
                break;
            case 1:
                url = url30232;
                break;
            case 2:
                url = url30216;
                break;
        }



        ds.down(url,request);




    }
    @RequestMapping(value = "/x/flv")
    public void flv(@RequestParam(defaultValue="0",required=false) int quality, String av, HttpServletRequest request, HttpServletResponse response){
        //String url ="https://xy14x104x230x146xy.mcdn.bilivideo.cn:4483/upgcxcode/08/39/334883908/334883908_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1621410514&gen=playurlv2&os=mcdn&oi=2102830156&trid=0001f9096b402e774dc7868216b4c01fa236u&platform=pc&upsig=4f68bc72eb343f45143d8e28f14acd6b&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mcdnid=2000740&mid=229162729&orderid=0,3&agrr=1&logo=A0000002";
        System.out.println("flv视频");
        JSONArray video = ds.getFlvVideo(av, request);
        String url = video.getJSONArray(0).getJSONObject(0).getString("url");



        ds.getFlv(url,request,response);




    }
}
